//
//  MyAttentionViewController.m
//  birthdayCalculate
//
//  Created by 宋浩文的pro on 15/12/14.
//  Copyright © 2015年 宋浩文的pro. All rights reserved.
//

#import "MyAttentionViewController.h"
#import "MyAttentionCell.h"
#import <BmobSDK/Bmob.h>
#import "CommonTool.h"
#import "UIView+Extension.h"
#import "MBProgressHUD.h"
#import "FavorateModel.h"
#import "ReadDetailViewController.h"
#import "FavorateViewController.h"

@interface MyAttentionViewController () <UITableViewDelegate, UITableViewDataSource, MyAttentionCellDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSArray *focusArray;

@end

@implementation MyAttentionViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setTableView];
    
    [self requestData];
    
    [self setNavi];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.focusArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyAttentionCell *cell = [MyAttentionCell cellWithTableView:tableView];
    BmobObject *bObject = self.focusArray[indexPath.row];
    FavorateModel *model = [[FavorateModel alloc] init];
    model.title = [bObject objectForKey:@"title"];
    model.source = [bObject objectForKey:@"source"];
    model.img = [bObject objectForKey:@"pic_url"];
    model.html_string = [bObject objectForKey:@"html_string"];
    cell.model = model;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 130;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    // 进入房间
    BmobObject *news = self.focusArray[indexPath.row];
    FavorateModel *model = [[FavorateModel alloc] init];
    model.title = [news objectForKey:@"title"];
    model.source = [news objectForKey:@"source"];
    model.img = [news objectForKey:@"pic_url"];
    model.html_string = [news objectForKey:@"html_string"];
    
    FavorateViewController *favorateVC = [[FavorateViewController alloc] init];
    favorateVC.model = model;
    [self.navigationController pushViewController:favorateVC animated:YES];
    
    // 取消选中效果
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}



#pragma mark - private methods
- (void)requestData
{
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"加载中...";
    
    BmobQuery *bquery = [BmobQuery queryWithClassName:@"News"];
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        
        self.focusArray = array;
        [self.tableView reloadData];
        [hud hide:YES];
    }];

}


- (void)setNavi
{
    self.navigationItem.title = @"我的收藏";
}

- (void)setTableView
{
    [self.view addSubview:self.tableView];
    
    _tableView.rowHeight = 80;
    _tableView.dataSource = self;
    _tableView.delegate = self;
}

#pragma mark - 懒加载
- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeigth - 64) style:UITableViewStylePlain];
        UIView *nullFooter = [[UIView alloc] initWithFrame:CGRectZero];
        [_tableView setTableFooterView:nullFooter];
    }
    return _tableView;
}

- (NSArray *)focusArray
{
    if (_focusArray == nil) {
        
        _focusArray = [NSArray array];
    }
    return _focusArray;
}

@end
